package com.ecarx.vhaltests.hy11.testcases.brightness;

import android.car.VehicleAreaSeat;
import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyConfig;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.xui.adaptapi.car.IWrapper;
import com.ecarx.xui.adaptapi.car.property.IProperty;
import com.ecarx.xui.adaptapi.car.vehicle.IDayMode;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


public class SettingFuncBrightnessTest extends BrightnessTestBase {

    private static final String TAG = "SettingFuncBrightnessTest";

    private static int mPropertyId = -1;
    private static int mAutoPropertyId = -1;
    private static final int mAREA = VehicleAreaSeat.SEAT_ROW_1_CENTER;
    private static final int mGlobal_AREA = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;


    @Before
    public void testInit() {
        IWrapper wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        mPropertyId = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IDayMode.SETTING_FUNC_BRIGHTNESS_SCREEN).getPropertyId();
        mAutoPropertyId = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, IProperty.SETTING_FUNC_BRIGHTNESS_AUTO_ADJUST).getPropertyId();
        DELAY_GETTING_TIME = 500;

        mockDayNightTwliBriSts(false);
        delay();
    }

    @Test
    public void testCSDBrightAdjManuSet_max_min() {

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();

        CarPropertyConfig<?> config = mCarPropertyMgr.getCarPropertyConfig(mPropertyId);
        Assert.assertEquals(config.getMaxValue(mAREA), 15);
        Assert.assertEquals(config.getMinValue(mAREA), 0);
    }

    @Test
    public void testCSDBrightAdjManuSet_normal() {
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();

        for (int i = 0; i < 16; ++i) {
            mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, i);
            delay();
            int brightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
            Assert.assertEquals(brightness, i);
        }

    }

    @Test
    public void testCSDBrightAdjManuSet_max_error() {
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();

        mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, 16);
        delay();
        int brightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness, 15);
    }

    @Test
    public void testCSDBrightAdjManuSet_min_error() {
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();

        mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, -2);
        delay();
        int brightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness, 0);
    }

    @Test
    public void testCSDBrightAdjManuSetOnAutoMode() {

        int SETTING_FUNC_BRIGHTNESS_AUTO_ADJUST = mAutoPropertyId;
        //tun on
        mCarPropertyMgr.setBooleanProperty(SETTING_FUNC_BRIGHTNESS_AUTO_ADJUST, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL, true);
        delay();
        boolean isAutoOn = mCarPropertyMgr.getBooleanProperty(SETTING_FUNC_BRIGHTNESS_AUTO_ADJUST, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        Assert.assertTrue(isAutoOn);
        //manual set
        mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, 9);
        delay();
        boolean isAutoOnAfter = mCarPropertyMgr.getBooleanProperty(SETTING_FUNC_BRIGHTNESS_AUTO_ADJUST, VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL);
        //check status
        Assert.assertFalse(isAutoOnAfter);
    }

    @Test
    public void testCSDBrightAdjAutoSet_Available() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(mAutoPropertyId, mGlobal_AREA);
        Log.d(TAG, "isAvail：" + isAvail);
        Assert.assertTrue(isAvail);
    }

    @Test
    public void testCSDBrightAdjAutoSet_onOff() {
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, true);
        delay();
        boolean ret = mCarPropertyMgr.getBooleanProperty(mAutoPropertyId, mGlobal_AREA);
        Assert.assertTrue(ret);
        delay();
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();
        ret = mCarPropertyMgr.getBooleanProperty(mAutoPropertyId, mGlobal_AREA);
        Assert.assertFalse(ret);
    }

    @Test
    public void testCSDBrightAdjAutoSet_normalCase_byStep_highEnd() {

        setIsLowEndModel(false);
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();
        mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, 0);
        delay();
        int brightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness, 0);

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, true);
        mockAmbIllmnFwdStsAmblillmn1And2(100, 0);
        delay(3000);
        int brightness1 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness1, 2);

        mockAmbIllmnFwdStsAmblillmn1And2(300, 0);
        delay(3000);
        int brightness2 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness2, 4);

        mockAmbIllmnFwdStsAmblillmn1And2(0, 8);
        delay(5000);
        int brightness3 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness3, 8);

        mockAmbIllmnFwdStsAmblillmn1And2(3000, 17);
        delay(5000);
        int brightness4 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness4, 12);

        mockAmbIllmnFwdStsAmblillmn1And2(0, 195);
        delay(3000);
        int brightness5 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness5, 14);

        delay();
    }


    @Test
    public void testCSDBrightAdjAutoSet_normalCase_lowEnd() {

        setIsLowEndModel(true);

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, true);
        delay();

        mockDayNightTwliBriSts(true);
        delay(5000);
        int dayBrightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(dayBrightness, 12);

        mockDayNightTwliBriSts(false);
        delay(5000);
        int nightBrightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(nightBrightness, 4);

        delay();
    }

    @Test
    public void testCSDBrightAdjAutoSet_setTo_manual_highEnd() {
        setIsLowEndModel(false);
        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();
        mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, 5);
        delay(3000);

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, true);
        delay();
        mockAmbIllmnFwdStsAmblillmn1And2(100, 0);
        delay(3000);
        int brightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness, 2);

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();
        int brightness1 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness1, 5);

    }

    @Test
    public void testCSDBrightAdjAutoSet_setTo_manual_lowEnd() {
        setIsLowEndModel(true);

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();
        mCarPropertyMgr.setIntProperty(mPropertyId, mAREA, 5);
        delay();
        int brightness = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness, 5);


        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, true);
        delay(4000);
        mockDayNightTwliBriSts(false);
        delay(4000);
        int brightness1 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness1, 4);

        mockDayNightTwliBriSts(true);
        delay(4000);
        int brightness2 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness2, 12);

        mCarPropertyMgr.setBooleanProperty(mAutoPropertyId, mGlobal_AREA, false);
        delay();
        int brightness3 = mCarPropertyMgr.getIntProperty(mPropertyId, mAREA);
        Assert.assertEquals(brightness3, 5);
        delay();
    }
}
